package com.shop2cn.iapi.prodstockmgr.testcase;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.shop2cn.iapi.prodstockmgr.parameter.GetInventoryDetailBean;
import com.shop2cn.iapi.prodstockmgr.service.GetInventoryDetailCall;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ProductDetailsDto;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.GodfreightmgrService;
import com.ymttest.business.service.Shop2cnStockCallService;
import com.ymttest.database.model.stockmgr.agentinventory;
import com.ymttest.database.model.stockmgr.agentinventoryExample;
import com.ymttest.database.model.stockmgr.agentinventorysku;
import com.ymttest.database.sqlwapper.stockmgr.agentinventoryWapper;
import com.ymttest.database.sqlwapper.stockmgr.agentinventoryskuWapper;
import com.ymttest.utils.AgentUsers;
import com.ymttest.utils.TestDataManager;
/**
*入/出库单详情Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetInventoryDetail{
	private static GetInventoryDetailBean getinventorydetailBean;
	private static GetInventoryDetailCall getinventorydetailCall;
	
	//add wuwen 20200205 
	agentinventoryExample example=new agentinventoryExample();
	agentinventoryWapper sku=new agentinventoryWapper();
	
	// 渠道商
	private static int rootUserId = AgentUsers.rootUserId;
	private static String rootUserName = "NickautoTest";
	// 渠道商->代理商1
	private static int agentUserId1 = AgentUsers.agentUserId1;
	private static String agentUserName1 = "NickautoTest";
	// vip渠道
	private static int vipRootUserId = AgentUsers.vipRootUserId;
	private static String vipRootUserName = "NickautoTest";
	private static int csId = AgentUsers.csId;
	// vip渠道->代理
	private static int vipAgentUserId1 = AgentUsers.vipAgentUserId1;
	private static String vipAgentUserName1 = "dgsq408897016";
	private static int csId1 = AgentUsers.csId1;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("入/出库单详情Test");
		try {
			GodfreightmgrService.deleteBySellerId(rootUserId);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Before
	public void caseUp() {
		getinventorydetailBean=new GetInventoryDetailBean();
		getinventorydetailCall=new GetInventoryDetailCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getinventorydetailCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void verifyDB(JsonObject asJsonObject,String productId) throws Exception {
		Logger.comment("数据库验证");
		String inventoryNo = asJsonObject.get("inventoryNo").getAsString();
		agentinventory agentinventory=new agentinventoryWapper().selectByInventoryNo(inventoryNo).get(0);
		agentinventorysku agentinventorysku= new agentinventoryskuWapper().selectByInventoryNo(inventoryNo).get(0);
		Logger.verifyEquals(asJsonObject.get("type").getAsString(), ""+ agentinventory.getType(), "验证type");
//		Logger.verifyEquals(asJsonObject.get("").getstr(), agentinventory.getType(), "验证创建时间");
		int num=agentinventorysku.getNum();
		Logger.verifyEquals(asJsonObject.get("totalNum").getAsInt(), num<0?-num:num, "验证totalNum");
		Logger.verifyEquals(asJsonObject.get("totalCost").getAsFloat(), agentinventorysku.getCost()==null?0.0f:agentinventorysku.getCost().floatValue()*num , "验证totalCost");
//		Logger.verifyEquals(asJsonObject.get("operator").getAsString(), ""+agentinventory.getOperatorId(), "验证operator");
		Logger.verifyEquals(asJsonObject.get("remark").isJsonNull()?null:asJsonObject.get("remark").getAsString(), agentinventory.getRemark(), "验证remark");
		Logger.verifyEquals(asJsonObject.get("orderId").getAsLong(), agentinventory.getOrderId()==null?0:agentinventory.getOrderId(), "验证orderId");
		ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
		JsonObject product = asJsonObject.getAsJsonArray("productList").get(0).getAsJsonObject();
		Logger.verifyEquals(product.get("mainPic").getAsString(), productDetailsDto.getPics().get(0), "验证mainpic");
		Logger.verifyEquals(product.get("productName").getAsString(), productDetailsDto.getTitle(), "验证商品名称");
		JsonObject catalog = product.getAsJsonArray("catalogList").get(0).getAsJsonObject();
		Logger.verifyEquals(catalog.get("propertyValue").getAsString(), productDetailsDto.getCatalogList().get(0).getPropertyValue(), "验证propertyValue");
		Logger.verifyEquals(catalog.get("sku").getAsString(), productDetailsDto.getCatalogList().get(0).getSku(), "验证sku");
		
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetInventoryDetail_001() {
		Logger.start(true,"查询发货出库详情，成功");
		try {
			 
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			//long stockOut = Shop2cnStockCallService.stockOutOnDelivery(rootUserId, productId, 10);
			//update wuwen 20200205
			long orderId = Shop2cnStockCallService.stockOutOnDelivery(rootUserId, productId, 10);
			Shop2cnStockCallService.stockOutOnDelivery(rootUserId, productId, 10);
			example.createCriteria().andOrderIdEqualTo(orderId);
			agentinventory sk=sku.selectByExample(example).get(0);
			
			String stockOut = sk.getInventoryNo().toString();
			getinventorydetailBean.setInventoryNo(""+stockOut);
			getinventorydetailBean.setSellerId(rootUserId);
			getinventorydetailCall.setData(getinventorydetailBean);
			getinventorydetailCall.callService();			
			Logger.verifyEquals("200", getinventorydetailCall.getString("code"), "验证code");
			
			verifyDB(getinventorydetailCall.getJsonObject().getAsJsonObject("data"),productId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetInventoryDetail_002() {
		Logger.start(true,"查询手动出库单详情，成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			String inventoryNo = Shop2cnStockCallService.stockOut(rootUserId, productId, 10);
			
			getinventorydetailBean.setInventoryNo(inventoryNo);
			getinventorydetailBean.setSellerId(rootUserId);
			getinventorydetailCall.setData(getinventorydetailBean);
			getinventorydetailCall.callService();			
			Logger.verifyEquals("200", getinventorydetailCall.getString("code"), "验证code");
			
			verifyDB(getinventorydetailCall.getJsonObject().getAsJsonObject("data"),productId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetInventoryDetail_003() {
		Logger.start(true,"查询手动入库详情，成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			String inventoryNo = Shop2cnStockCallService.stockIn(rootUserId, productId, 10);
			
			getinventorydetailBean.setInventoryNo(inventoryNo);
			getinventorydetailBean.setSellerId(rootUserId);
			getinventorydetailCall.setData(getinventorydetailBean);
			getinventorydetailCall.callService();			
			Logger.verifyEquals("200", getinventorydetailCall.getString("code"), "验证code");
			
			verifyDB(getinventorydetailCall.getJsonObject().getAsJsonObject("data"),productId);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetInventoryDetail_004() {
		Logger.start(true,"商家id为空，失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			long orderId = Shop2cnStockCallService.stockOutOnDelivery(rootUserId, productId, 10);
			
			getinventorydetailBean.setInventoryNo(""+orderId);
			getinventorydetailCall.setData(getinventorydetailBean);
			getinventorydetailCall.callService();			
			Logger.verifyEquals("400", getinventorydetailCall.getString("code"), "验证code");
			
			//verifyDB(getinventorydetailCall.getJsonObject().getAsJsonObject("data"),productId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetInventoryDetail_005() {
		Logger.start(true,"根据商家id查询，成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			long orderId = Shop2cnStockCallService.stockOutOnDelivery(rootUserId, productId, 10);
			
			getinventorydetailBean.setSellerId(rootUserId);
			getinventorydetailCall.setData(getinventorydetailBean);
			getinventorydetailCall.callService();			
			Logger.verifyEquals("400", getinventorydetailCall.getString("code"), "验证code");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetInventoryDetail_006() {
		Logger.start(true,"商家id与单号不匹配，失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			long orderId = Shop2cnStockCallService.stockOutOnDelivery(rootUserId, productId, 10);
			
			getinventorydetailBean.setInventoryNo(""+orderId);
			getinventorydetailBean.setSellerId(vipRootUserId);
			getinventorydetailCall.setData(getinventorydetailBean);
			getinventorydetailCall.callService();			
			Logger.verifyEquals("500", getinventorydetailCall.getString("code"), "验证code");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetInventoryDetail_007() {
		Logger.start(true,"查询4调整入库详情，成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			//0手动入库，1调整入库 新增 20200211 wuwen
			String inventoryNo = Shop2cnStockCallService.stockIn(rootUserId, productId, 10,1);
			
			getinventorydetailBean.setInventoryNo(inventoryNo);
			getinventorydetailBean.setSellerId(rootUserId);
			getinventorydetailCall.setData(getinventorydetailBean);
			getinventorydetailCall.callService();			
			Logger.verifyEquals("200", getinventorydetailCall.getString("code"), "验证code");
			
			verifyDB(getinventorydetailCall.getJsonObject().getAsJsonObject("data"),productId);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetInventoryDetail_008() {
		Logger.start(true,"查询调整出库单详情，成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			//0手动出库，1调整出库
			String inventoryNo = Shop2cnStockCallService.stockOut(rootUserId, productId, 10,1);
			getinventorydetailBean.setInventoryNo(inventoryNo);
			getinventorydetailBean.setSellerId(rootUserId);
			getinventorydetailCall.setData(getinventorydetailBean);
			        
			getinventorydetailCall.callService();			
			Logger.verifyEquals("200", getinventorydetailCall.getString("code"), "验证code");
			 
			verifyDB(getinventorydetailCall.getJsonObject().getAsJsonObject("data"),productId);
		} catch (Exception e) {
			Logger.fail(e); 
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetInventoryDetail_009() {
		Logger.start(true,"查询调整出库单详情，增加条形码");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			List<String> barCodes = Arrays.asList("A123_b123_"+TestDataManager.getRandomNum(10),"A123_b123_"+TestDataManager.getRandomNum(10));
			addAgentProductBean.getCatalogList().get(0).setBarcode(barCodes.get(0));;
			addAgentProductBean.getCatalogList().get(1).setBarcode(barCodes.get(1));;
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			//0手动出库，1调整出库
			String inventoryNo = Shop2cnStockCallService.stockOut(rootUserId, productId, 10,1);
			getinventorydetailBean.setInventoryNo(inventoryNo);
			getinventorydetailBean.setSellerId(rootUserId);
			getinventorydetailCall.setData(getinventorydetailBean);
			getinventorydetailCall.callService();
			Logger.verifyEquals("200", getinventorydetailCall.getString("code"), "验证code");
			
			JsonArray catalogList = getinventorydetailCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("productList").get(0).getAsJsonObject().getAsJsonArray("catalogList");
			Logger.verifyEquals(catalogList.get(0).getAsJsonObject().get("barcode").getAsString(), barCodes.get(0), "验证条形码");
		} catch (Exception e) {
			Logger.fail(e); 
		}
	}
	
	
}